package cn.wokoba.trigger.job;


import cn.wokoba.service.strategy.model.StrategyAwardStockKeyVO;
import cn.wokoba.service.strategy.repository.StrategyRepository;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


//更新奖品库存任务；异步队列更新数据库，保证最终一致。

@Slf4j
@Component
public class UpdateAwardStockJob {


    @Autowired
    StrategyRepository strategyRepository;

    //    @Scheduled(cron = "0/10 * * * * ?")
    @XxlJob("updateAwardStockJob")
    public void exec() {
        try {
            StrategyAwardStockKeyVO strategyAwardStockKeyVO = strategyRepository.takeQueueValue();
            if (null == strategyAwardStockKeyVO) return;
            log.info("定时任务，更新奖品消耗库存 strategyId:{} awardId:{}", strategyAwardStockKeyVO.getStrategyId(), strategyAwardStockKeyVO.getAwardId());
            strategyRepository.updateStrategyAwardStock(strategyAwardStockKeyVO.getStrategyId(), strategyAwardStockKeyVO.getAwardId());
        } catch (Exception e) {
            log.error("定时任务，更新奖品消耗库存失败", e);
        }
    }

}
